﻿using AutoMapper;
using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProductionManage.Models.Products;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using wangyaxin.Models;
using wangyaxin.Wang.Dto;
using wangyaxin.Wyx.Dto;
using Newtonsoft.Json;
using wangyaxin.Wyx;

namespace wangyaxin.Web.WyxController
{
    public class WyxController : Controller
    {

        private readonly IRepository<Product, int> _ProductRepository;
        private readonly WyxAppService _wyxAppService;
       /* private readonly IWyxAppService _wyxAppService*/ // 依赖注入 IWyxAppService
        private readonly IMapper _mapper; // 依赖注入 IMapper

        public WyxController(IRepository<Product, int> ProductRepository, IMapper mapper, WyxAppService wyxAppService)
        {
            _ProductRepository = ProductRepository;
            _mapper = mapper;
            _wyxAppService= wyxAppService;
        }


        public IActionResult Product()
        {
            return View();
        }

        public IActionResult Productupdate()
        {
            return View();
        }


        /// <summary>
        /// 获取产品列表--王亚鑫
        /// </summary>
        /// <param name="ProductName"></param>
        /// <param name="ProductCode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> GetProductList(string? ProductName, string? ProductCode, int pageIndex, int pageSize)
        {
            var list= await _wyxAppService.GetProduct(ProductName, ProductCode, pageIndex, pageSize);
            return list;
        }


        /// <summary>
        /// 添加产品--王亚鑫
        /// </summary>
        /// <param name="input">产品数据传输对象</param>
        /// <returns></returns>
        public  IActionResult AddProductData(CreateProductDto createProductDto)
        {
           var ok = _wyxAppService.AddProduct(createProductDto);
           return  Json("ok");
        }

        /// <summary>
        /// 删除产品--王亚鑫
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <returns></returns>
        public IActionResult DeleteProductData(int id)
        {
            var result = _wyxAppService.DeleteProduct(id);
            return Json(result);
        }




        /// <summary>
        /// 恢复产品--王亚鑫
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <returns></returns>
        [HttpGet]
        public  async Task<Product> GetProductByIdData(int id)
        {
            var result = await _wyxAppService.GetProductById(id);
            return result;
        }


        /// <summary>
        /// 更新产品--王亚鑫
        /// </summary>
        /// <param name="input">产品数据传输对象</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> UpdateProductData(UpdateProductDto product)
        {
          var result = await _wyxAppService.UpdateProduct(product);
            return Json(new
            {
                code = "200",
                msg = "更新成功",
            });
        }





    }

}
